<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车辆自编号" prop="seqno" label-width="100px">
        <el-input v-model="queryParams.seqno" placeholder="请输入车辆自编号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="车牌号码" prop="lpno">
        <el-input v-model="queryParams.lpno" placeholder="请输入车牌号码" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="佛山通行证办理日期" prop="foshanPermitDate" label-width="150px">
        <el-date-picker clearable v-model="queryParams.foshanPermitDate" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择佛山通行证办理日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="佛山通行证截止期限" prop="foshanExpiryDate" label-width="150px">
        <el-date-picker clearable v-model="queryParams.foshanExpiryDate" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择佛山通行证截止期限">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="农商中心库通行证办理日期" prop="nongshangPermitDate" label-width="200px">
        <el-date-picker clearable v-model="queryParams.nongshangPermitDate" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择农商中心库通行证办理日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="农商中心库通行证截止期限" prop="nongshangExpiryDate" label-width="200px">
        <el-date-picker clearable v-model="queryParams.nongshangExpiryDate" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择农商中心库通行证截止期限">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">查询</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['service:vehicle:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['service:vehicle:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['service:vehicle:remove']">删除</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="success" plain icon="Document" :disabled="single" @click="handleUpdate"
          v-hasPermi="['service:vehicle:edit']">导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Document" :disabled="single" @click="handleUpdate"
          v-hasPermi="['service:vehicle:edit']">导入模板</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Document" :disabled="single" @click="handleUpdate"
          v-hasPermi="['service:vehicle:edit']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Document" :disabled="single" @click="handleUpdate"
          v-hasPermi="['service:vehicle:edit']">佛山通行证起止日期导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Document" :disabled="single" @click="handleUpdate"
          v-hasPermi="['service:vehicle:edit']">佛山通行证起止日期导入模板</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Document" :disabled="single" @click="handleUpdate"
          v-hasPermi="['service:vehicle:edit']">农商中心库通行证起止日期导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Document" :disabled="single" @click="handleUpdate"
          v-hasPermi="['service:vehicle:edit']">农商中心库通行证起止日期导入模板</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="EditPen" :disabled="single" @click="handleUpdate"
          v-hasPermi="['service:vehicle:edit']">取消通行证过期提醒</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Document" :disabled="single" @click="handleUpdate"
          v-hasPermi="['service:vehicle:edit']">车辆年审日期导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Document" :disabled="single" @click="handleUpdate"
          v-hasPermi="['service:vehicle:edit']">车辆年审日期导入模板</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['service:vehicle:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="vehicleList" @selection-change="handleSelectionChange">
      <el-table-column label="" align="center" type="index" width="50" />
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="车辆自编号" align="center" prop="seqno" width="100"/>
      <el-table-column label="车牌号码" align="center" prop="lpno" />
      <el-table-column label="车辆型号" align="center" prop="model" />
      <el-table-column label="车辆性质" align="center" prop="vhType" />
      <el-table-column label="车辆类型" align="center" prop="vehicleType" />
      <el-table-column label="标准箱数" align="center" prop="standardBoxcount" />
      <el-table-column label="车辆状态" align="center" prop="statusT" />
      <el-table-column label="所属业务" align="center" prop="businessType" />
      <el-table-column label="下次维保日期" align="center" prop="nextMaintainDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.nextMaintainDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否已维保" align="center" prop="maintainResult" width="100"/>
      <el-table-column label="所属机构" align="center" prop="departmentId" />
      <el-table-column label="车辆年审日期" align="center" prop="examinedDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.examinedDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车辆年审到期日期" align="center" prop="examinedExpiryDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.examinedExpiryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="佛山通行证办理日期" align="center" prop="foshanPermitDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.foshanPermitDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="佛山通行证截止期限" align="center" prop="foshanExpiryDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.foshanExpiryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="佛山通行证办理内容" align="center" prop="foshanPermitContent" width="180"/>
      <el-table-column label="农商中心库通行证办理日期" align="center" prop="nongshangPermitDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.nongshangPermitDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="农商中心库通行证截止期限" align="center" prop="nongshangExpiryDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.nongshangExpiryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="农商中心库通行证办理内容" align="center" prop="nongshangPermitContent" width="180" />
      <el-table-column label="取消通行证过期提醒日期" align="center" prop="permitExpiryReminder" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['service:vehicle:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['service:vehicle:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改车辆管理对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="vehicleRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="车辆自编号" prop="seqno"  label-width="200px">
          <el-input v-model="form.seqno" placeholder="请输入车辆自编号" />
        </el-form-item>
        <el-form-item label="车牌号码" prop="lpno" label-width="200px">
          <el-input v-model="form.lpno" placeholder="请输入车牌号码" />
        </el-form-item>
        <el-form-item label="车辆型号" prop="model" label-width="200px">
          <el-input v-model="form.model" placeholder="请输入车辆型号" />
        </el-form-item>
        <el-form-item label="标准箱数" prop="standardBoxcount" label-width="200px">
          <el-input v-model="form.standardBoxcount" placeholder="请输入标准箱数" />
        </el-form-item>
        <el-form-item label="车辆状态" prop="statusT" label-width="200px">
          <el-input v-model="form.statusT" placeholder="请输入车辆状态" />
        </el-form-item>
        <el-form-item label="下次维保日期" prop="nextMaintainDate" label-width="200px">
          <el-date-picker clearable v-model="form.nextMaintainDate" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择下次维保日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="车辆年审日期" prop="examinedDate" label-width="200px">
          <el-date-picker clearable v-model="form.examinedDate" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择车辆年审日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="车辆年审到期日期" prop="examinedExpiryDate" label-width="200px">
          <el-date-picker clearable v-model="form.examinedExpiryDate" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择车辆年审到期日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="佛山通行证办理日期" prop="foshanPermitDate" label-width="200px">
          <el-date-picker clearable v-model="form.foshanPermitDate" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择佛山通行证办理日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="佛山通行证截止期限" prop="foshanExpiryDate" label-width="200px">
          <el-date-picker clearable v-model="form.foshanExpiryDate" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择佛山通行证截止期限">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="佛山通行证办理内容" label-width="200px">
          <editor v-model="form.foshanPermitContent" :min-height="192" />
        </el-form-item>
        <el-form-item label="农商中心库通行证办理日期" prop="nongshangPermitDate" label-width="200px">
          <el-date-picker clearable v-model="form.nongshangPermitDate" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择农商中心库通行证办理日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="农商中心库通行证截止期限" prop="nongshangExpiryDate" label-width="200px">
          <el-date-picker clearable v-model="form.nongshangExpiryDate" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择农商中心库通行证截止期限">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="农商中心库通行证办理内容" label-width="200px">
          <editor v-model="form.nongshangPermitContent" :min-height="192" />
        </el-form-item>
        <el-form-item label="取消通行证过期提醒日期" prop="permitExpiryReminder" label-width="200px">
          <el-input v-model="form.permitExpiryReminder" placeholder="请输入取消通行证过期提醒日期" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Vehicle">
import { listVehicle, getVehicle, delVehicle, addVehicle, updateVehicle } from "@/api/service/vehicle"

const { proxy } = getCurrentInstance()

const vehicleList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    seqno: null,
    lpno: null,
    foshanPermitDate: null,
    foshanExpiryDate: null,
    nongshangPermitDate: null,
    nongshangExpiryDate: null,
  },
  rules: {
    seqno: [
      { required: true, message: "车辆自编号不能为空", trigger: "blur" }
    ],
    lpno: [
      { required: true, message: "车牌号码不能为空", trigger: "blur" }
    ],
    model: [
      { required: true, message: "车辆型号不能为空", trigger: "blur" }
    ],
    vhType: [
      { required: true, message: "车辆性质不能为空", trigger: "change" }
    ],
    vehicleType: [
      { required: true, message: "车辆类型不能为空", trigger: "change" }
    ],
    standardBoxcount: [
      { required: true, message: "标准箱数不能为空", trigger: "blur" }
    ],
    statusT: [
      { required: true, message: "车辆状态不能为空", trigger: "blur" }
    ],
    deleted: [
      { required: true, message: "删除标志不能为空", trigger: "blur" }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询车辆管理列表 */
function getList() {
  loading.value = true
  listVehicle(queryParams.value).then(response => {
    vehicleList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    seqno: null,
    lpno: null,
    model: null,
    vhType: null,
    vehicleType: null,
    standardBoxcount: null,
    statusT: null,
    businessType: null,
    nextMaintainDate: null,
    maintainResult: null,
    departmentId: null,
    examinedDate: null,
    examinedExpiryDate: null,
    foshanPermitDate: null,
    foshanExpiryDate: null,
    foshanPermitContent: null,
    nongshangPermitDate: null,
    nongshangExpiryDate: null,
    nongshangPermitContent: null,
    permitExpiryReminder: null,
    keysCount: null,
    keysDetail: null,
    instock: null,
    location: null,
    kilometers: null,
    keyholderId: null,
    /* nottaskApplyId: null,
    nottaskApplyTime: null,
    nottaskApplyReason: null,
    nottaskApproveStatus: null,
    nottaskApproveId: null,
    nottaskApproveTime: null, */
    frameNumber: null,
    engineNumber: null,
    emissionStandard: null,
    productionDate: null,
    enrollDate: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
    deleted: null
  }
  proxy.resetForm("vehicleRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加车辆管理"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getVehicle(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改车辆管理"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["vehicleRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateVehicle(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addVehicle(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除车辆管理编号为"' + _ids + '"的数据项？').then(function() {
    return delVehicle(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('service/vehicle/export', {
    ...queryParams.value
  }, `vehicle_${new Date().getTime()}.xlsx`)
}

getList()
</script>
